﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Schema;
using PathologicalGames;
using UnityEngine;
using UnityEngine.UI;

public class MySkillPoolContainer : MonoBehaviour
{
    public static MySkillPoolContainer _instance;
    private SpawnPool IconPool;
    private Transform IconPoolTran;
    private Transform SkillPoolIconPrefab;
    private Transform MyTran;
    private Dictionary<ushort,Transform> IconInstanceDic = new Dictionary<ushort, Transform>();
    private Dictionary<ushort,Transform> BanSkillDic = new Dictionary<ushort, Transform>();

    //private ushort[] iconIDs = new ushort[] { 101, 102, 103, 104, 105, 106, 107, 201, 202, 203, 204, 301 };
    //private ushort index;

    void Awake()
    {
        _instance = this;
        IconPool = PoolManager.Pools["SkillIcon"];
        SkillPoolIconPrefab = IconPool.prefabs["SkillPoolIcon"];
        IconPoolTran = IconPool.transform;
        MyTran = transform;
    }
    
    /// <summary>
    /// 添加一个新图标到我的技能池内
    /// </summary>
    /// <param name="SkillID"></param>
    public void PutIconInPool(ushort SkillID)
    {
        Transform IconTran = IconPool.Spawn(SkillPoolIconPrefab, MyTran);
        MySkillPoolIcon PoolIcon = IconTran.GetComponent<MySkillPoolIcon>();
        PoolIcon.SetInfo(SkillID);
        IconInstanceDic.Add(SkillID,IconTran);
    }

    /// <summary>
    /// 放入批量技能到技能池
    /// </summary>
    /// <param name="SkillIDArr"></param>
    public void PutIconsInPool(ushort[] SkillIDArr)
    {
        for (int i = 0; i < SkillIDArr.Length; i++)
        {
            PutIconInPool(SkillIDArr[i]);
        }
    }

    /// <summary>
    /// 清除(回收)一个技能图标
    /// </summary>
    /// <param name="SkillID"></param>
    public void FlagMyPoolIcon(ushort SkillID,bool IsBan)
    {
        //先检查这个技能还在不在当前技能池字典内，在的话就删除，不在就不处理
        Transform IconInstanceTran = new RectTransform();
        bool isInDic = IconInstanceDic.TryGetValue(SkillID,out IconInstanceTran);
        if (isInDic)
        {
            BanSkillDic.Add(SkillID,IconInstanceTran);
            //将按钮变灰，不能点击
            Image img = IconInstanceTran.GetComponent<Image>();
            img.raycastTarget = false;
            img.color = IsBan?Color.gray: new Color(0, 0.87f, 1);
            IconInstanceDic.Remove(SkillID);
        }
    }

    public void OnDisable()
    {
        IconInstanceDic.Clear();
        Transform[] SkillArr = BanSkillDic.Values.ToArray();
        for (int i = 0; i < SkillArr.Length; i++)
        {
            Image img = SkillArr[i].GetComponent<Image>();
            img.raycastTarget = true;
            img.color = Color.white;
        }
    }
}
